#pragma semicolon 1 // Force strict semicolon mode.

#include <sourcemod>
#include <sdktools>
#include <adminmenu>
#include <psycore>

#define PLUGIN_NAME              "tPsyMenu"
#define PLUGIN_AUTHOR            "Thrawn, psyduck"
#define PLUGIN_VERSION           "1.0.0.0"
#define PLUGIN_CONTACT           "http://aaa.einfachonline.net/"

new Handle:g_EffectMenu = INVALID_HANDLE;

new g_iMenuEffect[MAXPLAYERS+1];


public OnPluginStart()
{
	RegConsoleCmd("sm_psyfx_effect", Command_ApplyEffect);
}
  
public OnMapEnd()
{
	if (g_EffectMenu != INVALID_HANDLE)
	{
		CloseHandle(g_EffectMenu);
		g_EffectMenu = INVALID_HANDLE;
	}
}
 
Handle:BuildEffectMenu()
{ 
	/* Create the menu Handle */
	new Handle:menu = CreateMenu(MenuHandler_SelectEffect);
	
	new count = psyRTD_GetEffectCount();
	for(new i = 0; i < count; i++) {
		new eID = psyRTD_GetEffectID(i);
		
		new String:eName[64];
		psyRTD_GetEffectName(eID,eName);
		new String:eIDString[4];
		IntToString(eID, eIDString, 4);
		
		AddMenuItem(menu, eIDString, eName);
	}
		
	/* Finally, set the title */
	SetMenuTitle(menu, "Please select an effect:");
 
	return menu;
}
 
public MenuHandler_SelectEffect(Handle:menu, MenuAction:action, client, param2)
{
	if (action == MenuAction_Select)
	{
		new String:info[32];
 
		/* Get item info */
		GetMenuItem(menu, param2, info, sizeof(info));
 		new eID = StringToInt(info);
 		
 		g_iMenuEffect[client] = eID;

		new String:eName[64];
		psyRTD_GetEffectName(eID,eName);
 		 		
 		//Open the target menu
		new Handle:targetmenu = CreateMenu(MenuHandler_ApplyEffect);

		decl String:title[100];
		Format(title, sizeof(title), "Apply %s on:", eName);
		SetMenuTitle(targetmenu, title);
		//SetMenuExitBackButton(menu, true);

		AddTargetsToMenu(targetmenu, client, true, true);

		DisplayMenu(targetmenu, client, MENU_TIME_FOREVER); 		
	}
}

public MenuHandler_ApplyEffect(Handle:menu, MenuAction:action, param1, param2)
{
	if (action == MenuAction_End)
	{
		CloseHandle(menu);
	}
	else if (action == MenuAction_Select)
	{
		decl String:info[32];
		new userid, target;
		
		GetMenuItem(menu, param2, info, sizeof(info));
		userid = StringToInt(info);

		if ((target = GetClientOfUserId(userid)) == 0)
		{
			PrintToChat(param1, "[SM] %t", "Player no longer available");
		}
		else if (!CanUserTarget(param1, target))
		{
			PrintToChat(param1, "[SM] %t", "Unable to target");
		}
		else if (!IsPlayerAlive(target))
		{
			ReplyToCommand(param1, "[SM] %t", "Player has since died");
		}	
		else
		{
			decl String:name[32];
			GetClientName(target, name, sizeof(name));

			new String:eName[64];
			psyRTD_GetEffectName(g_iMenuEffect[param1],eName);
			
			new psyRTDAction:result = psyRTD_DoEffect(target, g_iMenuEffect[param1]);
			if(result == psyRTD_Reroll) {
				ReplyToCommand(param1, "[SM] Could not apply %s on %N", eName, target);
			} else {			
				ShowActivity2(param1, "[SM] ", "Applied %s on %N", eName, target);
			}						
		}
	}
} 
 
public Action:Command_ApplyEffect(client, args)
{
	if (g_EffectMenu != INVALID_HANDLE)
	{
		CloseHandle(g_EffectMenu);
	}

	g_EffectMenu = BuildEffectMenu();	
	DisplayMenu(g_EffectMenu, client, MENU_TIME_FOREVER);
 
	return Plugin_Handled;
}